import { ref } from 'vue';

type UploadSuccessCallback = (res : any) => void;

interface UploadOptions {
	url : string;
	filePath : string;
	name : string;
	success ?: UploadSuccessCallback;
	data : {}
}

export function useUpload() {
	const imgList = ref([]);

	async function uploadFile(options : UploadOptions) {
		try {
			const res = await uni.uploadFile({
				url: options.url,
				filePath: options.filePath,
				name: options.name,
				formData: options.data,
			});

			if (options.success) {
				options.success(res);
			}

			imgList.value.push(res.data);
		} catch (e) {
			console.error('上传失败', e);
		}
	}

	return {
		imgList,
		uploadFile,
	};
}